	package com.hera.base.util;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vaadin.server.BootstrapFragmentResponse;
import com.vaadin.server.BootstrapListener;
import com.vaadin.server.BootstrapPageResponse;
import com.vaadin.server.CustomizedSystemMessages;
import com.vaadin.server.ServiceException;
import com.vaadin.server.SessionInitEvent;
import com.vaadin.server.SessionInitListener;
import com.vaadin.server.SystemMessages;
import com.vaadin.server.SystemMessagesInfo;
import com.vaadin.server.SystemMessagesProvider;

public class VaadinServlet extends com.vaadin.server.VaadinServlet {

	private static final long serialVersionUID = 1L;

	@Override
    public void servletInitialized() throws ServletException {
        super.servletInitialized();
        
        getService().setSystemMessagesProvider(
        			new SystemMessagesProvider() {
						private static final long serialVersionUID = 1L;
						@Override
						public SystemMessages getSystemMessages(SystemMessagesInfo systemMessagesInfo) {
							 CustomizedSystemMessages messages = new CustomizedSystemMessages();
						        messages.setSessionExpiredCaption("Su sesión ha terminado, favor de ingresar de nuevo.");
						        messages.setCommunicationErrorCaption("Su sesión ha terminado, favor de ingresar de nuevo.");
						        messages.setInternalErrorCaption("Error Interno en la aplicación");
						        messages.setOutOfSyncCaption("Problema en la sincronización");
						        messages.setCookiesDisabledCaption("cookies estan desabilitadas");
						        messages.setAuthenticationErrorCaption("error de autenticación ");
						        return messages;
						}
        			}
        		);
        
        getService().addSessionInitListener(new SessionInitListener() {
			private static final long serialVersionUID = 1L;

			@Override
            public void sessionInit(SessionInitEvent event) throws ServiceException {
               event.getSession().addBootstrapListener(new BootstrapListener() {
            	   	private static final long serialVersionUID = 1L;
            	   	@Override
            	   	public void modifyBootstrapPage(BootstrapPageResponse response) {
	                    response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "./VAADIN/themes/admin/js/validaciones.js");
	                    response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "./VAADIN/themes/admin/js/jquery.numeric.js");
	                    response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "./VAADIN/themes/admin/js/jquery-2.1.1.js");
	                    
	                    //response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "./VAADIN/themes/admin/js/jalava/Jalava.js");
	                    //response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "./VAADIN/themes/admin/js/jalava/DOM.js");
	                    //response.getDocument().head().prependElement("script").attr("type", "text/javascript").attr("src", "./VAADIN/themes/admin/js/jalava/json2.js");
            	   	}
            	   	@Override
            	   	public void modifyBootstrapFragment(BootstrapFragmentResponse response) {}
               });
            }
         });
    }
	
	@Override
    protected void service(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
		/*
		if((request.getServletContext().getContextPath() + "/").equalsIgnoreCase(request.getRequestURI())){
			if(isThisRequestCommingFromAMobileDevice(request)){
				//System.out.println(browserDetails);
				response.sendRedirect(request.getServletContext().getContextPath() + "/movil/inbox.do");
			}else{
				//super.service(request, response);
				response.sendRedirect(request.getServletContext().getContextPath() + "/UI");
			}
		}else{*/
			if(!request.getRequestURI().contains("flowdesigner")){			
				super.service(request, response);
			}
		//}
		
		
	}
	
	private boolean isThisRequestCommingFromAMobileDevice(HttpServletRequest request){

	    String userAgent = request.getHeader("User-Agent");
	    String httpAccept = request.getHeader("Accept");

	    UAgentInfo detector = new UAgentInfo(userAgent, httpAccept);

	    if (detector.detectMobileQuick()) {
	        return true;
	    }

	    return false;
	}	
	
}
